cmake_minimum_required(VERSION 3.15)
project(BridgeTest LANGUAGES CXX)

# 定义程序版本号
set(BRIDGE_TEST_VERSION_MAJOR 0)
set(BRIDGE_TEST_VERSION_MINOR 0)
set(BRIDGE_TEST_VERSION_PATCH 1)
set(BRIDGE_TEST_VERSION "${BRIDGE_TEST_VERSION_MAJOR}.${BRIDGE_TEST_VERSION_MINOR}.${BRIDGE_TEST_VERSION_PATCH}")

# 传递版本号给源码（通过宏定义）
add_definitions(
    -DBRIDGE_TEST_VERSION="${BRIDGE_TEST_VERSION}"
    -DBRIDGE_TEST_VERSION_MAJOR=${BRIDGE_TEST_VERSION_MAJOR}
)

# 设置C++标准
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

# 设置C标准
set(CMAKE_C_STANDARD 17)
set(CMAKE_C_STANDARD_REQUIRED ON)

message(STATUS "CMAKE_SOURCE_DIR: ${CMAKE_SOURCE_DIR}")
cmake_path(GET CMAKE_SOURCE_DIR PARENT_PATH SOLUTION_DIR)
message(STATUS "SOLUTION_DIR: ${SOLUTION_DIR}")

# 配置编译选项
if(MSVC)
    add_compile_options(
        /W3
        /sdl
        /permissive-
        /wd4200 /wd4244  # 禁用特定警告
    )
    
    # 根据配置类型设置不同的编译选项
    if(CMAKE_BUILD_TYPE STREQUAL "Debug")
        add_compile_definitions(
            BOOST_ALL_NO_LIB
            BOOST_ALL_DYN_LINK
            BOOST_STACKTRACE_LINK
            BOOST_STACKTRACE_USE_ADDR2LINE
            BOOST_STACKTRACE_USE_WINDBG
            _DEBUG
            _CONSOLE
        )
    else()
        add_compile_definitions(
            BOOST_ALL_NO_LIB
            BOOST_ALL_DYN_LINK
            BOOST_STACKTRACE_LINK
            BOOST_STACKTRACE_USE_ADDR2LINE
            BOOST_STACKTRACE_USE_WINDBG
            NDEBUG
            _CONSOLE
        )
    endif()

    # 64位架构额外优化（可选）
    if(ARCH STREQUAL "x64")
        add_compile_options(/arch:AVX2)  # 针对64位CPU优化
    endif()
else()
    add_compile_options(
        -Wall
        -Wextra
        -Wno-pedantic        
        -Wno-unknown-pragmas
        -Wno-unused-parameter
        -Wno-missing-field-initializers
        -Wno-unused-variable
        -fPIC  # 生成位置无关代码，用于动态库
    )
    # 针对 Clang 的额外选项
    if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
        add_compile_options(-stdlib=libc++)
    endif()
     # 根据配置类型设置不同的编译选项
    if(CMAKE_BUILD_TYPE STREQUAL "Debug")
        add_compile_definitions(
            BOOST_ALL_NO_LIB
            BOOST_ALL_DYN_LINK
            _DEBUG
            _CONSOLE
        )
    else()
        add_compile_definitions(
            BOOST_ALL_NO_LIB
            BOOST_ALL_DYN_LINK
            NDEBUG
            _CONSOLE
        )
    endif()
endif()

# 设置包含目录
set(INCLUDE_DIRS
    /usr/include
    ${SOLUTION_DIR}/libs/include
    ${SOLUTION_DIR}/libs/include/jdbc
    ${SOLUTION_DIR}
    ${CMAKE_SOURCE_DIR}
)

include_directories(${INCLUDE_DIRS})

# 设置源文件
set(SOURCES
    ${SOLUTION_DIR}/BridgeAnalysis/Sampling.cpp
    ${SOLUTION_DIR}/BridgeAnalysis/utils.cpp
    ${CMAKE_SOURCE_DIR}/BridgeTest.cpp
    ${CMAKE_SOURCE_DIR}/test_analysis1.cpp
    ${CMAKE_SOURCE_DIR}/test_analysis2.cpp
    ${CMAKE_SOURCE_DIR}/test_chemdll.cpp
    ${CMAKE_SOURCE_DIR}/test_fileop.cpp
    ${CMAKE_SOURCE_DIR}/test_org.cpp
    ${CMAKE_SOURCE_DIR}/test_method.cpp
    ${CMAKE_SOURCE_DIR}/test_sampling.cpp
    ${CMAKE_SOURCE_DIR}/pch.cpp
)

# 设置头文件
set(HEADERS
    ${CMAKE_SOURCE_DIR}/ChemDevLayer.h
    ${CMAKE_SOURCE_DIR}/Bridge6Analysis.h
)

# === 修复：正确设置链接路径和库 ===
# 设置目标架构（默认x64，可通过-DARCH=Win32覆盖）
set(ARCH "x64" CACHE STRING "目标架构：Win32或x64")

if(MSVC)
    set(INSTALL_DIR "${SOLUTION_DIR}/${ARCH}/${CMAKE_BUILD_TYPE}")
else()
    set(INSTALL_DIR "${SOLUTION_DIR}/Linux/${CMAKE_BUILD_TYPE}")
endif()
message(STATUS "安装路径: ${INSTALL_DIR}")

# 设置输出目录
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)
set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR}/lib)

# 设置链接库
if(WIN32)
    # 基于ARCH变量设置库路径（删除手动覆盖的部分）
    set(LIB_DIR ${SOLUTION_DIR}/libs/lib/${ARCH})
    
    if(CMAKE_BUILD_TYPE STREQUAL "Debug")
        set(LIB_DIR ${LIB_DIR}/Debug)
    else()
        set(LIB_DIR ${LIB_DIR}/Release)
    endif()    

    message(STATUS "LIB_DIR: ${LIB_DIR}")
    link_directories(${LIB_DIR})    
    link_directories(${INSTALL_DIR})
    link_directories(${SOLUTION_DIR}/libs)    
    add_executable(BridgeTest ${SOURCES} ${HEADERS})

    message(STATUS "复制库路径: ${LIB_DIR}")
    install(FILES
        ${SOLUTION_DIR}/libs/ChemDevLayerDLL.dll
        ${SOLUTION_DIR}/libs/BridgeAnalysis.ini
        ${LIB_DIR}/Microsoft.DTfW.DHL.manifest
        DESTINATION "${INSTALL_DIR}"
    )
    install(DIRECTORY
        ${LIB_DIR}/
        DESTINATION "${INSTALL_DIR}"
        FILES_MATCHING PATTERN "*.dll"
    )
else()
    set(LIB_DIR ${SOLUTION_DIR}/libs/lib/linux)

    if(CMAKE_BUILD_TYPE STREQUAL "Debug")
        set(LIB_DIR ${LIB_DIR}/Debug)
    else()
        set(LIB_DIR ${LIB_DIR}/Release)
    endif()

    if(CMAKE_BUILD_TYPE STREQUAL "Debug")
        set(HIREDIS_LIB "hiredisd")
    else()
        set(HIREDIS_LIB "hiredis")
    endif()
    set(BOOST_LIBS
        boost_filesystem
        boost_log
        boost_log_setup
        boost_thread
        boost_atomic
        boost_json
        boost_locale
        # boost_system
    )
    set(OTHER_LIBS
        mysqlcppconn
        redis++
        gtest
        gtest_main
        ${HIREDIS_LIB}
        pthread  # Linux 线程库
        BridgeAnalysis
        ChemDLL
    )

    message(STATUS "LIB_DIR: ${LIB_DIR}")
    link_directories(/usr/lib/aarch64-linux-gnu)    
    link_directories(${LIB_DIR})  
    link_directories(${INSTALL_DIR})

    add_executable(BridgeTest ${SOURCES} ${HEADERS})
    target_link_libraries(BridgeTest
        ${BOOST_LIBS}
        ${OTHER_LIBS}
    )
    install(FILES
        /usr/lib/aarch64-linux-gnu/libmariadb.so.3
        ${SOLUTION_DIR}/libs/BridgeAnalysis.ini
        DESTINATION "${INSTALL_DIR}"
    )
    install(DIRECTORY
        ${LIB_DIR}/
        DESTINATION "${INSTALL_DIR}"
        FILES_MATCHING PATTERN "lib*.so*"
    )
    set_target_properties(BridgeTest PROPERTIES
        OUTPUT_NAME "BridgeTest-${BRIDGE_TEST_VERSION}"  # 文件名带版本
    )
endif()

set(CMAKE_PCH_ENABLED TRUE)
target_precompile_headers(BridgeTest PRIVATE 
    ${SOLUTION_DIR}/BridgeTest/pch.h
)

# 确保安装路径存在（递归创建）
file(MAKE_DIRECTORY ${INSTALL_DIR})

# 安装配置
install(TARGETS BridgeTest
    RUNTIME DESTINATION "${INSTALL_DIR}"
    LIBRARY DESTINATION "${INSTALL_DIR}"
    ARCHIVE DESTINATION "${INSTALL_DIR}"
)


# 设置Windows目标平台版本
if(WIN32)
    set(CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION "10.0")
    set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
endif()

# Windows下32位构建
# "x86 Native Tools Command Prompt for VS 2022"下，下一行命令可省略，已经执行过了。
# "D:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars32.bat"
# ############# 构建Windows Win32 Release版本 ######################
# cd D:\Work\Bridge6\BridgeTest
# mkdir build\win32_release
# cd build/win32_release
# cmake ..\.. -G "Ninja" -DCMAKE_BUILD_TYPE=Release -DARCH=Win32 
# ninja -v -j8
# ninja install
# ############# 构建Windows Win32 Debug版本 ######################
# cd D:\Work\Bridge6\BridgeTest
# mkdir build\win32_debug
# cd build/win32_debug
# cmake ..\.. -G "Ninja" -DCMAKE_BUILD_TYPE=Debug -DARCH=Win32
# ninja -v -j8
# ninja install

# Windows下64位构建
# "x64 Native Tools Command Prompt for VS 2022"下，下一行命令可省略，已经执行过了。
# "D:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars32.bat"
# ############# 构建Windows x64 Release版本 ######################
# cd D:\Work\Bridge6\BridgeTest
# mkdir build\x64_release
# cd build/x64_release
# cmake ..\.. -G "Ninja" -DCMAKE_BUILD_TYPE=Release -DARCH=x64 
# ninja -v -j8
# ninja install
# ############# 构建Windows x64 Debug版本 ######################
# cd D:\Work\Bridge6\BridgeTest
# mkdir build\x64_debug
# cd build/x64_debug
# cmake ../.. -G "Ninja" -DCMAKE_BUILD_TYPE=Debug -DARCH=x64
# ninja -v -j8
# ninja install

# Linux下64位构建
# ############# 构建Linux x64 Release版本 ######################
# cd /root/work/bridge6/BridgeTest
# mkdir -p build/linux_x64_release
# cd build/linux_x64_release
# cmake ../.. -G "Ninja" -DCMAKE_BUILD_TYPE=Release -DARCH=aarch64 -DCMAKE_CXX_COMPILER_LAUNCHER=ccache
# ninja -v -j$(nproc)
# ninja install
# ############# 构建Linux x64 Debug版本 ######################
# cd /root/work/bridge6/BridgeTest
# mkdir -p build/linux_x64_debug
# cd build/linux_x64_debug
# cmake ../.. -G "Ninja" -DCMAKE_BUILD_TYPE=Debug -DARCH=aarch64 -DCMAKE_CXX_COMPILER_LAUNCHER=ccache
# ninja -v -j$(nproc)
# ninja install